<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 案例浏览接口
 */
class DesignScan extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    public function getDesignScanList()
    {
        $pageIndex = $this->request->post('pageIndex', 1);
        $pageSize = $this->request->post('pageSize', 10);
        $startDate = $this->request->post('startDate');
        $endDate = $this->request->post('endDate');
        $user = $this->auth->getUser();
        // 超级管理员
        $where = [];
        if ($user['group_id'] == 2) {
            $where['user_id'] = $user['id'];
        }
        if ($startDate && $endDate) {
            $where['design_scan.update_time'] = ['between time', [$startDate, $endDate . ' 23:59:59']];
        }
        $list = model('\app\admin\model\jy\DesignScan')
            ->field('*, DATE_FORMAT(design_scan.update_time, "%Y-%m-%d") create_date, count(design_scan.user_id) scan_count')
            ->with('design')
            ->where('design.is_del = 0')
            ->where($where)
            ->group('DATE_FORMAT(design_scan.update_time, "%Y-%m-%d"), design_scan.design_id')
            ->order('create_date desc')
            ->paginate($pageSize, false, ['page' => $pageIndex]);
        $newList = [];
        if ($list) {
            foreach ($list as $item) {
                $newList[] = [
                    'design_id'    => $item['design_id'],
                    'date'         => date('Y年m月d日', strtotime($item['create_time'])),
                    'design_name'  => $item['design']['design_name'],
                    'cover_images' => cdnurl($item['design']['cover_images']),
                    'scan_count'   => $item['scan_count']
                ];
            }
        }
        $result = array("total" => $list->total(), "rows" => $newList);
        $this->success('', $result);
    }

    public function getDesignScanListByDesignId()
    {
        $pageIndex = $this->request->post('pageIndex', 1);
        $pageSize = $this->request->post('pageSize', 10);
        $designId = $this->request->post('designId');
        $user = $this->auth->getUser();
        // 是否是超级管理员
        if ($user['group_id'] == 2) {
            $this->error(__('You have no permission'));
        }
        //当前是否为关联查询
        $this->relationSearch = true;
        $list = model('\app\admin\model\jy\DesignScan')
            ->field('*')
            ->with(['user'])
            ->where('design_scan.design_id', $designId)
            ->order('design_scan.update_time desc')
            ->paginate($pageSize, false, ['page' => $pageIndex]);
        $newList = [];
        if ($list) {
            foreach ($list as $item) {
                $newList[] = [
                    'id'        => $item['id'],
                    'date'      => date('Y年m月d日', strtotime($item['update_time'])),
                    'time'      => date('G:i', strtotime($item['update_time'])),
                    'design_id' => $item['design_id'],
                    'user_id'   => $item['user']['id'],
                    'avatar'    => cdnurl($item['user']['avatar']),
                    'nickname'  => $item['user']['nickname'],
                ];
            }
        }
        $result = array("total" => $list->total(), "rows" => $newList);
        $this->success('', $result);
    }
}
